(function(window, $){
	var basePath='http://newmbs.bj.unicom.local:9085/attach';
	var _baseHtmlPath = basePath+'/static/html/folder';
	var _baseOperatePath = basePath+'/netdisk/folder';
	var _baseRemotePath = basePath+'/remoteApi/Folder';
	var _dataGridId = "dataGrid";
	var isIE = typeof(window.opera)!="object"&&window.navigator.userAgent.indexOf("MSIE")>0;
	
	folder = {};
	
	folder.operate = {
		searchInfo:function(parentId){
			
			$.ajax({
				url:_baseOperatePath+"/selectNextAll",
				type:'post',
				data:{"parentId":parentId},
				success:function(){
					$("#iframeHome").attr("src",basePath+"/main/toHomePage?parentId="+parentId);
				}
			});
//			if(isIE){
			    try{
			      var doc = $("#history_iframe").contentWindow.document;
			      doc.open();
			      doc.write('<html><body><div id="state">'+parentId+'</div></body></html>');
			      doc.close();
			    }catch(e){}
//			  }
			parent.location.hash = parentId;
		},
		searchField:function(searchField){
			$.ajax({
				url:_baseOperatePath+"/searchField",
				type:"post",
				data:{"searchField":searchField},
				success:function(){
					$("#iframepage",window.parent.document).contents().find("#iframeHome").css("charset","utf-8");
					$("#iframepage",window.parent.document).contents().find("#iframeHome").attr("src",basePath+"/main/toSearch?searchField="+encodeURI(searchField));
					//window.reload();
				}
			});
		},	
		moveChecked:function(){
			var strs = $(window.frames["iframeHome"].document.getElementsByName("box"));
			var checkIds = "";
			for(var i=0 ; i<strs.length;i++){
				if(strs[i].checked == true){
					checkIds += strs[i].value +',';
				}
			}
			if(checkIds == ""){
				alert("请至少选择一个选项！");
			}else{
				folder.operate.move(checkIds);
			}
		},
		move:function(moveField){
			//下载至网盘
		 //   window.open(_baseRemotePath+'/tomove','','height=400,width=500,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no'); 
			//从网盘中选择文件上传
		//	window.open(_baseRemotePath+'/tomoveUpload','','height=400,width=500,top=0,left=0,toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no'); 
		    //移动
		 	var _parentContent = $('<div></div>');
			_parentContent.load(_baseOperatePath+'/tomove?moveField='+moveField+'&title=0');
			BootstrapDialog.show({
	            title: '移动文件夹',
	            message: _parentContent,
	            onshown:function(){
	            	folder.Dialog.initAdd(_parentContent);
	            },
	            buttons: [{
	                label: '保存',
	                action: function(dialogRef){
	                	
	                	var endField = $('#endField').val();
	                	if(endField != "" && endField != null){
	                		
	                		$.ajax({
	            				type : "post",
	            				url : _baseOperatePath+'/upmove?moveField='+moveField+"&endField="+endField,     
	            				dataType : "text"
	            			})
	            			.done(function(data) {
	            				if(data=='true'){
	            					alert("移动成功！");
	            					//window.location.reload();
	            					dialogRef.close();
	            					$("#details >a:last-child ").click();
	            				}else {
	            					BootstrapDialog.alert("移动失败！");
	            				}
	            				
	            			})
	          			.fail(function() {
	          				BootstrapDialog.alert("移动失败！");//后台报错的话就提示执行这个 
	          				});
	                	}else{
		                	//alert("失败！");
	                		BootstrapDialog.alert("失败！"); 
	                	}
	            	
	                }
	            }, {
	                label: '取消',
	                action: function(dialogRef){
	                    dialogRef.close();
	                }
	            }],
	            cssClass:'bsubmit',
	            draggable: true
	        });
		},	
		remoteAdd:function(folderName,pid,userId){ 	//对远程调用的树进行新增操作
			$.ajax({
				type : "post",
				url : _baseRemotePath+'/toAddFolder?folderName='+encodeURI(encodeURI(folderName))+'&folderPid='+pid+'&userId='+userId,     
			})
			.done(function(date) {
					var zTree = $.fn.zTree.getZTreeObj("treeDemo"),	//刷新id
					nodes = zTree.getSelectedNodes(),
					treeNode = nodes[0];
					treeNode.id=date;
					$("#endField").val(treeNode.id);
			})
			.fail(function() {
				alert("增加失败！");//后台报错的话就提示执行这个 
			});
		},
		remoteEditName:function(folderName,folderId){//对远程调用的树进行修改操作
			$.ajax({
				type : "post",
				url : _baseRemotePath+'/upFolderName?folderId='+folderId+'&folderName='+encodeURI(encodeURI(folderName)),    
				dataType : "json"
			})
			.done(function(data) {
					alert("修改成功");
					window.opener.location.reload();
					window.location.reload();
			})
			.fail(function() {
				alert("修改失败！");//后台报错的话就提示执行这个 
				});
		},
		remotedel:function(checkIds){//对远程调用的树进行删除操作
			$.ajax({
				type:"post",
				url:_baseOperatePath+'/delByFileId',
				data:{'checkIds':checkIds},
				dataType:"json"
			})
			.done(function(){
				alert("删除成功");
				window.opener.location.reload();
				window.location.reload();
			})
			.fail(function(){
				BootstrapDialog.alert("删除失败！"); 
			});
		},
		copyFiles:function(checkIds){
			//alert("复制操作checkIds="+checkIds);
			var _parentContent = $('<div></div>');
			_parentContent.load(_baseOperatePath+'/tomove?moveField='+checkIds+'&title=2');
			BootstrapDialog.show({
	            title: '复制文件夹',
	            message: _parentContent,
	            onshown:function(){
	            	folder.Dialog.initAdd(_parentContent);
	            },
	            buttons: [{
	                label: '保存',
	                action: function(dialogRef){
	                	//alert("123")
	                	var folderId = $('#endField').val();
	                	if(folderId != "" && folderId != null){
	                		
	                		$.ajax({
	            				type : "post",
	            				url : _baseOperatePath+'/copyFiles?checkIds='+checkIds+"&folderId="+folderId,     
	            				dataType : "text"
	            			})
	            			.done(function(data) {
	            				if(data=='true'){
	            				alert("复制成功！");
	            				dialogRef.close();
	            				$("#details >a:last-child ").click();
	            				}else {
	            					BootstrapDialog.alert("复制失败！");
	            				}
	            				
	            			})
	          			.fail(function() {
	          				BootstrapDialog.alert("复制失败！");//后台报错的话就提示执行这个 
	          				});
	                	}else{
		                	alert("失败！");
	                		BootstrapDialog.alert("失败！"); 
	                	}
	            	
	                }
	            }, {
	                label: '取消',
	                action: function(dialogRef){
	                    dialogRef.close();
	                }
	            }],
	            cssClass:'bsubmit',
	            draggable: true
	        });
		},
		add:function(folderId){
			var _parentContent = $('<div></div>');
			_parentContent.load(_baseHtmlPath+'/folderdata.html');
			BootstrapDialog.show({
	            title: '新建文件夹',
	            message: _parentContent,
	            onshown:function(){
	            	folder.Dialog.initAdd(_parentContent);
	            },
	            buttons: [{
	                label: '保存',
	                action: function(dialogRef){
	                	var _form = $('#form');
	                	if(_form.validationEngine('validate')){
	                		folder.operate.saveRecord(dialogRef,_form,folderId);
	                	}else{
	                		return;
	                	}
	                }
	            }, {
	                label: '取消',
	                action: function(dialogRef){
	                    dialogRef.close();
	                }
	            }],
	            cssClass:'bsubmit',
	            draggable: true
	        });
		},
		
		edit:function(fileId){
			$.ajax({
				url:_baseOperatePath+'/selectByfileId',
				method:'post',
				data:{"fileId":fileId},
				dataType : "json",
			})
			.done(function(data){
				BootstrapDialog.show({
					title:'编辑数据',
					message:$("<div></div>").load(_baseHtmlPath+'/folderdata.html'),
					
					onshown:function(result){
						$("#folderName").attr("value",data.folderName);
						$("#folderDesc").attr("value",data.folderDesc);
					},
					buttons:[{
						label:'保存',
						action:function(dialogRef){
							var _form = $('#form');
							if(_form.validationEngine('validate')){
		                		folder.operate.saveEdit(dialogRef, _form, fileId);
		                	}else{
		                		return;
		                	}
						}
					},{
						label:'取消',
						action:function(dialogRef){
							dialogRef.close();
						}
					}]
				});
				
			})
			.fail(function(){
				alert(fileId);
				BootstrapDialog.alert("失败！"); 
			});		
		},
		
		del:function(checkIds){
			$.ajax({
				type:"post",
				url:_baseOperatePath+'/delByFileId',
				data:{'checkIds':checkIds},
				dataType:"json"
			})
			.done(function(){
				alert("删除成功");
				window.location.reload();
			})
			.fail(function(){
				BootstrapDialog.alert("删除失败！"); 
			});
		},
		
		saveEdit:function(_defDialog,_form,fileId){
			$.ajax({
				type : "post",
				url : _baseOperatePath+'/updateByFileId?fileId='+fileId,
				data: _form.serialize(),
				dataType : "json"
			})
			.done(function(){
				_defDialog.close();
				window.location.reload();
			})
			.fail(function(){
				BootstrapDialog.alert("保存失败！"); 
			});
		},
		
		saveRecord:function(_defDialog,_form,_folderId){
			$.ajax({
				type : "post",
				url : _baseOperatePath+'/saveFolder?parentId='+_folderId,
				data: _form.serialize(),
				dataType : "json"
			})
			.done(function() {
				//alert("123");
				_defDialog.close();
				var name =document.getElementsByName("select");
				if(name.length > 0){
					var folderId = name[name.length-1].id;
				 	folder.operate.searchInfo(folderId);
				}else{
					window.location.reload(true);
				}
				//
				//folder.operate.search();
				//window.reload();
				//
				
			})
			.fail(function() {
				BootstrapDialog.alert("保存失败！"); });
		
		}
	};
	
	folder.Dialog = {
			initValidate:function(){
				$('#form').validationEngine('attach',{
			        maxErrorsPerField: 1,
			        scroll: false, 
			        validationEventTrigger:"keyup blur change focus",
			        ValidateNoneVisibleFields: true
			    });
			},
			initAdd: function (_parentContent){
				folder.Dialog.initValidate();
			},
			initEdit:function (_parentContent,_editId){
				folder.Dialog.initValidate();
				$.ajax({
					type : "get",
					url : _baseOperatePath+'/listDefaultDisplay',
					data: {},
					dataType : "json"
				})
				.done(function(r) {
					$.each(r,function(i,v){
						$('#defaultDisplay').append('<option value="'+v.key+'">'+v.value+'</option>');
					});
					$.ajax({
						type : "get",
						url : _baseOperatePath+'/getById',
						data: {'id':_editId},
						dataType : "json"
					})
					.done(function(r) { 
						if(r.result){
							$('#id').val(r.data.id);
			            	$('#personName').val(r.data.personName);
			            	$('#taxpayerCode').val(r.data.taxpayerCode);
			            	$('#defaultDisplay').val(r.data.defaultDisplay);
			            	$('#lastModifyId').val(r.data.lastModifyId);
			            	$('#lastModifyName').val(r.data.lastModifyName);
			            	$('#lastModifyTime').val(personNameList.Dialog.getCurrentDate());
						}
					});
					
				});
			},
			initValidate:function(){
				
			}
	};
	
	folder.Grid = {
			getGrid:function(){
				return $('#' + _dataGridId);
			},
			getCheckedData:function(){
				return folder.Grid.getGrid().datagrid('getChecked');
			}
	};
	
	window.folder = folder;
	
})(window, jQuery);